package com.demo.gateway.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.demo.gateway.mapper.RoutesMapper;
import com.demo.gateway.pojo.GatewayRouteDO;
import com.demo.gateway.util.JsonUtil;

/**
 * @author: demo
 * @date: 2020年6月22日
 * @Document : 数据库路由操作类
 */
@Service
public class DatabaseRouteServcie {
	private static final int GROW_RANGE = 1;
	@Autowired
	private RoutesMapper routesMapper;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * 因为filters中含有#{} mybatis会识别为入参
	 * 
	 * @param route
	 */
	public String addOrUpdateRoute(GatewayRouteDO route) {
		String id = route.getId();
		String sql = "";
		String filters = JSONObject.toJSONString(route.getFilters());
		String predicates = JSONObject.toJSONString(route.getPredicates());
		String location = route.getLocation();
		int order = routesMapper.queryRouteOrder(id);
		if (order > 0) {
			// 不修改order
			route.setOrder(order);
			sql = "update  gateway_routes set uri=?,filters=?,predicates=?,application_name=?,version=?,status=?,start_time=?,method_type=?,location=?,description=?  where id=?";
			jdbcTemplate.update(sql, route.getUri(), filters, predicates, route.getApplicationName(),
					route.getVersion(), route.getStatus(), route.getStartTime(), route.getMethodType(),
					route.getLocation(), route.getDescription(), id);
			return "路由更新成功!";
		} else {
			order = routesMapper.queryMaxRouteOrder(location);
			order += GROW_RANGE;
			route.setOrder(order);
			sql = "insert into gateway_routes(id,uri,filters,predicates,orders,application_name,version,status,start_time,method_type,location,description) values (?,?,?,?,?,?,?,?,?,?,?,?)";
			jdbcTemplate.update(sql, id, route.getUri(), filters, predicates, route.getOrder(),
					route.getApplicationName(), route.getVersion(), route.getStatus(), route.getStartTime(),
					route.getMethodType(), route.getLocation(), route.getDescription());
			return "路由添加成功!";
		}
	}

	public void deleteRoute(String id) {
		routesMapper.deleteRoute(id);
	}

	/**
	 * 获取所有路由信息
	 * 
	 * @return
	 */
	public List<GatewayRouteDO> queryDatabaseRoutes() {
		String sql = "select * from gateway_routes";
//		List<GatewayRoutePOJO> routeLists = jdbcTemplate.query(sql, new RowMapper<GatewayRoutePOJO>() {
//			@Override
//			public GatewayRoutePOJO mapRow(ResultSet rs, int rowNum) throws SQLException {
//				GatewayRoutePOJO routeBean=new GatewayRoutePOJO();
//				routeBean.setId(rs.getString("id"));
//				routeBean.setOrder(rs.getInt("orders"));
//				routeBean.setFilters(JsonUtil.toListBean(rs.getString("filters"), FilterDefinition.class));
//				routeBean.setPredicates(JsonUtil.toListBean(rs.getString("predicates"), PredicateDefinition.class));
//				routeBean.setUri(rs.getString("uri"));
//				return routeBean;
//			}
//		});
		List<GatewayRouteDO> routeLists = jdbcTemplate.query(sql, (rs, rowNum) -> {
			GatewayRouteDO routeBean = new GatewayRouteDO();
			routeBean.setId(rs.getString("id"));
			routeBean.setOrder(rs.getInt("orders"));
			routeBean.setFilters(JsonUtil.toListBean(rs.getString("filters"), FilterDefinition.class));
			routeBean.setPredicates(JsonUtil.toListBean(rs.getString("predicates"), PredicateDefinition.class));
			routeBean.setUri(rs.getString("uri"));
			routeBean.setApplicationName(rs.getString("application_name"));
			routeBean.setMethodType(rs.getString("method_type"));
			routeBean.setStartTime(rs.getDate("start_time"));
			routeBean.setStatus(rs.getInt("status"));
			routeBean.setVersion(rs.getString("version"));
			routeBean.setLocation(rs.getString("location"));
			routeBean.setDescription(rs.getString("description"));
			return routeBean;
		});
		return routeLists;
	}
}
